package com.wsx.rpc.thrift;

import com.wsx.rpc.thrift.impl.TtsPythonServiceImpl;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.server.TNonblockingServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.server.TThreadedSelectorServer;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TServerSocket;

/**
 * @author wangshuaixin
 * @Title: com.wsx.rpc.thrift.ThriftApplication
 * @Description: TODO
 * @date 2019/01/04
 */
public class ThriftApplication {

    public static void main(String[] args) {

        int port = 8491;
        TServer server = null;

        try {
            //transport 传输层，网络
            TServerSocket serverSocket = new TServerSocket(port);
            //protocol 协议层
            TBinaryProtocol.Factory factory = new TBinaryProtocol.Factory(true, true);
            //业务实际的handler
            TtsPythonServiceImpl handler = new TtsPythonServiceImpl();
            TtsPythonService.Processor<TtsPythonServiceImpl> processor = new TtsPythonService.Processor<TtsPythonServiceImpl>(handler);

            //服务层，对外提供服务
            server = new TThreadPoolServer(
                    new TThreadPoolServer.Args(serverSocket)
                        .protocolFactory(factory)
                        .processor(processor)
            );
            //启动监听
            server.serve();

        } catch (Exception e) {
            e.printStackTrace();
        }

        ///startNoBlock();
    }

    public static void startNoBlock() {
        int port = 8492;
        TServer server = null;

        try {
            //transport 传输层，网络，非阻塞的服务
            TNonblockingServerSocket serverSocket = new TNonblockingServerSocket(port);
            //protocol 协议层，高密度二进制协议，异步io需要TFramedTransport来进行分块的读取
            TCompactProtocol.Factory factory = new TCompactProtocol.Factory();
            //业务实际的handler
            TtsPythonServiceImpl handler = new TtsPythonServiceImpl();
            TtsPythonService.Processor<TtsPythonServiceImpl> processor = new TtsPythonService.Processor<TtsPythonServiceImpl>(handler);

            //服务层，对外提供服务
            server = new TNonblockingServer(
                    new TNonblockingServer.Args(serverSocket)
                            .protocolFactory(factory)
                            .transportFactory(new TFramedTransport.Factory())
                            .processor(processor)
            );
            //启动监听
            server.serve();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void startSelect() {
        int port = 8493;
        TServer server = null;

        try {
            //transport 传输层，网络，非阻塞的服务
            TNonblockingServerSocket serverSocket = new TNonblockingServerSocket(port);
            //protocol 协议层，高密度二进制协议，异步io需要TFramedTransport来进行分块的读取
            TBinaryProtocol.Factory factory = new TBinaryProtocol.Factory();
            //业务实际的handler
            TtsPythonServiceImpl handler = new TtsPythonServiceImpl();
            TtsPythonService.Processor<TtsPythonServiceImpl> processor = new TtsPythonService.Processor<TtsPythonServiceImpl>(handler);

            //服务层，对外提供服务
            server = new TThreadedSelectorServer(
                    new TThreadedSelectorServer.Args(serverSocket)
                            .protocolFactory(factory)
                            .transportFactory(new TFramedTransport.Factory())
                            .processor(processor)
            );
            //启动监听
            server.serve();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
